﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SPPReportaToExcel.bussines;
using System.Text;
using System.IO;
using System.Web.UI.HtmlControls;

namespace SPPReportaToExcel.VWPReportToExcel
{
    public partial class VWPReportToExcelUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        private void goToExcel()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            EscribeCabecera(sw);
            EscribePiePagina(sw);


            HtmlTextWriter htw = new HtmlTextWriter(sw);

            Page page = new Page();

            //HtmlForm form = new HtmlForm();
            //GridView1.EnableViewState = false;
            // Deshabilitar la validación de eventos, sólo asp.net 2
            page.EnableEventValidation = false;
            // Realiza las inicializaciones de la instancia de la clase Page que requieran los diseñadores RAD.
            page.DesignerInitialize();

            //page.Controls.Add(form);
            //form.Controls.Add(GridView1);

            page.RenderControl(htw);

            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", "attachment;filename=data.xls");
            Response.Charset = "UTF-8";
            Response.ContentEncoding = Encoding.Default;
            Response.Write(sb.ToString());
            Response.End();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            goToExcel();
        }

        public void EscribeCabecera(StringWriter saida)
        {
            StringBuilder html = new StringBuilder();
            html.Append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
            html.Append("<html>");
            html.Append("<head>");
            html.Append("<title>Report - ANAJUR</title>");
            html.Append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
            html.Append("</head>");
            html.Append("<body>");
            html.Append("<table>");
            html.Append("<tr style=\"font-weight:bold;font-size: 12px;color: white;\">");
            html.Append("<td></td>");
            html.Append("<td bgcolor='Blue'>Titulo de la tabla:</td>");
            html.Append("<td bgcolor='Blue'>Iteración:</td>");
            html.Append("</tr>");

            saida.Write(html.ToString());
        }
        
        public void EscribePiePagina(StringWriter saida)
        {
            StringBuilder html = new StringBuilder();
            html.Append("</table>");
            html.Append("<small>by ANAJUR</small>");
            html.Append("</body>");
            html.Append("</html>");
            saida.Write(html.ToString());
        }
    }
}
